package com.zycfc.zsf.boot.druid;

import com.alibaba.druid.pool.*;
import java.sql.*;
import com.zycfc.zsf.boot.util.text.*;
import com.zycfc.zsf.boot.common.enums.*;
import java.util.*;
import java.util.regex.*;
import org.slf4j.*;

public class ZsfDruidDataSource extends DruidDataSource
{
    private static final Logger log;
    
    public DruidAbstractDataSource.PhysicalConnectionInfo createPhysicalConnection() throws SQLException {
        final String url = this.getUrl();
        final List<String> list = this.getFields("%", url);
        ZsfDruidDataSource.log.info("org1-url:{}", (Object)url);
        if (!StringUtils.isEmpty((List)list)) {
            final String newUrl = this.parseTxt(url);
            ZsfDruidDataSource.log.info("new-Url:{}", (Object)newUrl);
            this.setUrl(newUrl);
        }
        return super.createPhysicalConnection();
    }
    
    private String parseTxt(String txt) {
        final List<String> paramters = this.getFields("%", txt);
        String val = "";
        for (final String p : paramters) {
            if ("zsf.boot.ds.suffix".equals(p)) {
                final String addSuffixStr = String.valueOf(YamlUtils.instance.getValueByKey("application.yml", "zsf.boot.ds.addSuffix"));
                ZsfDruidDataSource.log.info("addSuffixStr:{}", (Object)addSuffixStr);
                if (StringUtils.isEmptyAll(addSuffixStr) || "true".equals(addSuffixStr)) {
                    final String activeProfile = this.getActiveProfile();
                    ZsfDruidDataSource.log.info("activeProfile:{}", (Object)activeProfile);
                    val = "_" + activeProfile;
                }
                else {
                    val = String.valueOf(YamlUtils.instance.getValueByKey("application.yml", p));
                }
            }
            txt = txt.replace("%{" + p + "}", val);
        }
        return txt;
    }
    
    private List<String> getFields(final String txt) {
        return this.getFields("$", txt);
    }
    
    public String getActiveProfile() {
        final Object key = YamlUtils.instance.getValueByKey("application.yml", "spring.profiles.active");
        final List<String> list = this.getFields(String.valueOf(key));
        if (StringUtils.isEmpty((List)list)) {
            ZsfDruidDataSource.log.error("spring.profiles.active\u53c2\u6570\u4e3a\u7a7a");
            return "";
        }
        final String profileStr = list.get(0).split(":")[1];
        final String profile = ZsfNameSpace.getProfile(profileStr.split(","));
        return profile;
    }
    
    private List<String> getFields(final String prefix, final String txt) {
        final List<String> list = new ArrayList<String>();
        final Pattern regex = Pattern.compile("\\" + prefix + "\\{([^}]*)\\}");
        final Matcher matcher = regex.matcher(txt);
        while (matcher.find()) {
            list.add(matcher.group(1));
        }
        return list;
    }
    
    static {
        log = LoggerFactory.getLogger((Class)ZsfDruidDataSource.class);
    }
}
